<?
    $lang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    $pattern_ru = '/^(.*)ru(.*)$/';
    $pattern_md = '/^(.*)md(.*)$/';
    if (preg_match($pattern_ru, $lang[0])) {
        header ('Location: http://ru.' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    } elseif (preg_match($pattern_md, $lang[0])){
        header ('Location: http://md.' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    } else {
        header ('Location: http://en.' . $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    }
?>